package com.gx.smart.building.ui.common.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.gx.smart.base.BaseAdapter;
import com.gx.smart.building.R;
import com.gx.smart.building.ui.common.model.AttachmentInfo;

/**
 * @author xiaosy
 * @create 2020/5/12
 * @Describe
 **/
public class AttachmentAdapter extends BaseAdapter<AttachmentInfo, AttachmentAdapter.ViewHolder> {

    public AttachmentAdapter(Context context) {
        super(context);
    }

    @Override
    protected int onBindLayout() {
        return R.layout.item_attachment_layout;
    }

    @Override
    protected ViewHolder onCreateHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    protected void onBindData(ViewHolder holder, AttachmentInfo attachmentInfo, int position) {
        switch (attachmentInfo.getType()) {
            case 1://本地图片
                holder.addImage.setVisibility(View.GONE);
                holder.idItemAttachmentDelete.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(attachmentInfo.getUri()).into(holder.idItemAttachmentImage);
                holder.idTvRefresh.setVisibility(attachmentInfo.isFail() ? View.VISIBLE : View.GONE);
                break;
            case 2://add添加图片
                holder.idItemAttachmentDelete.setVisibility(View.GONE);
                holder.addImage.setVisibility(View.VISIBLE);
                break;
            case 3://网络图片
                holder.addImage.setVisibility(View.GONE);
                holder.idItemAttachmentDelete.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(attachmentInfo.getUrl()).into(holder.idItemAttachmentImage);
                holder.idTvRefresh.setVisibility(View.GONE);
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getType();
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView idItemAttachmentImage;
        private final TextView idTvRefresh;
        private final ImageView idItemAttachmentDelete;
        private final View addImage;

        public ViewHolder(View view) {
            super(view);
            idItemAttachmentImage = (ImageView) view.findViewById(R.id.id_item_attachment_image);
            idTvRefresh = (TextView) view.findViewById(R.id.id_tv_refresh);
            idItemAttachmentDelete = (ImageView) view.findViewById(R.id.id_item_attachment_delete);
            addImage = view.findViewById(R.id.addImage);
        }
    }
}
